import java.io.*;
import java.util.ArrayList;

public class Category {
	private String Name;
	private ArrayList<File> List;
	private int OriginalNameLength;
	
	public Category(String name, ArrayList<File> list){
		this.Name=name;
		this.List=list;
		this.OriginalNameLength=name.length();
	}
	public Category(String name){
		this.Name=name;
		this.List=new ArrayList<File>();
		this.OriginalNameLength=name.length();
	}
	public Category(File firstFile){
		this.Name=firstFile.getName();
		this.List=new ArrayList<File>();
		this.OriginalNameLength=this.Name.length();
		this.List.add(firstFile);
	}
	public String getName(){
		return this.Name;
	}
	public ArrayList<File> getFileList(){
		return this.List;
	}
	public int getSize(){
		return this.List.size();
	}
	public void setName(String name){
		this.Name=name;
	}
	public void setList(ArrayList<File> list){
		this.List=list;
	}
	public File getFile(int i){
		return this.List.get(i);
	}
	public int FirstNameLength(){
		return this.OriginalNameLength;
	}
	public void addFile(File target){
		this.List.add(target);
	}
	public String getFileName(int i){
		return this.List.get(i).getName();
	}
	public static boolean suitability(Category CheckCate, String CheckFileName, int fitness){
		if(CheckFileName.toLowerCase().contains(CheckCate.getName().toLowerCase())){
			return true;
		}
		else if(NameFitting(CheckCate, CheckFileName, CheckCate.getName().length(), 0, fitness)){
			return true;
		}
		else{
			return false;
		}
	}
	
	private static boolean NameFitting(Category CateName, String FileName, int CheckLength, int CheckStart, int fitness){
		//System.out.println(CheckLength+"  "+CheckStart);
		String CheckName = CateName.getName().substring(CheckStart,CheckStart+CheckLength);
		if(CheckLength<CateName.FirstNameLength()*fitness/100){
			return false;
		}
		else if(FileName.contains(CheckName)){
			CateName.setName(fixingName(CheckName));
			return true;
		}
		else{
			try{
				if(NameFitting(CateName, FileName, CheckLength, CheckStart+1, fitness)){
					return true;
				}
				else{
					return false;
				}
			}
			catch(StringIndexOutOfBoundsException e){
				if(NameFitting(CateName, FileName, CheckLength-1, 0, fitness)){
					return true;
				}
				else{
					return false;
				}
			}
		}
	}
	public static String fixingName(String fixName){
		int endPoint=fixName.length()-1;
		while(true){
			if((fixName.charAt(endPoint)==' ')||(fixName.charAt(endPoint)=='-')){
				endPoint--;
			}
			else{break;}
		}
		return fixName.substring(0, endPoint+1);
	}
	
}
